fix: clear value-list state when fan-level has value-range#1729
Open
Simonvoss97 wants to merge 1 commit into
Open
fix: clear value-list state when fan-level has value-range#1729Simonvoss97 wants to merge 1 commit into
Simonvoss97 wants to merge 1 commit into
Conversation
When a fan device exposes two `fan-level` properties on the same service - one with `value-list` and one with `value-range` - the `Fan.__init__` would set `_speed_name_map`/`_speed_names` from the value-list prop, then later let the value-range prop override only `_prop_fan_level` (per the existing precedence rule). The stale value-list state would still satisfy the value-list branch in the `percentage` property, causing `KeyError` for any stepless level that is not a key in the gear map. The exception propagates out of `state_attributes`, so the entity is stuck reporting `unknown` and the device cannot be controlled from HA. Clear `_speed_names` and `_speed_name_map` in the value-range branch so the percentage property falls back to `ranged_value_to_percentage`, which is what the in-code precedence comment already promises. Reproduces on xiaomi.fan.p76 and xiaomi.fan.p70 (both ship gear fan-level at piid=4 and stepless fan-level at piid=5).
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1728.
Problem
When a fan device exposes two
fan-levelproperties on the same MiOT service - one withvalue-list(gear) and one withvalue-range(stepless) - theFanentity ends up with mixed internal state:_prop_fan_levelcorrectly points to the stepless prop (per the existing precedence rule),_speed_names/_speed_name_mapstill hold the gear value-list from the earlier iteration.The
percentageproperty guards onif self._speed_names and self._speed_name_map, which stays truthy, so it goes down the value-list branch and callsself._speed_name_map[fan_level]. When the device reports a stepless level (e.g. 25 onxiaomi.fan.p76, 35 onxiaomi.fan.p70) the key isn't in the gear map and aKeyErroris raised duringasync_write_ha_state->state_attributes. The entity is stuck reportingunknownand can't be controlled.Fix
Clear
_speed_names/_speed_name_mapin the value-range branch ofFan.__init__, so thepercentageproperty falls back toranged_value_to_percentage- which is what the in-code comment already promises ("Fan level with value-range is prior to fan level with value-list").Reproduction
Devices affected (both verified locally):
xiaomi.fan.p76-urn:miot-spec-v2:device:fan:0000A005:xiaomi-p76:1:0000D062xiaomi.fan.p70-urn:miot-spec-v2:device:fan:0000A005:xiaomi-p70:1:0000D062Both expose
fan-levelatpiid=4(value-list[0..3]) andpiid=5(value-range[1,100,1]).Verification
fan.xiaomi_de_..._p76_s_2_fanwas stuck inunknown; logs showedKeyError: 25(or35for p70) infan.py:314whenever the device pushed a state update.state=on,percentage=10,preset_mode=Straight Wind, control works from HA.Diff
if prop.value_range: # Fan level with value-range ... self._attr_supported_features |= FanEntityFeature.SET_SPEED self._prop_fan_level = prop + # Clear any value-list state set by a previously processed + # value-list fan-level prop, so that the percentage property + # does not pick the value-list branch with a stale map. + self._speed_names = [] + self._speed_name_map = {} elif ( self._prop_fan_level is None and prop.value_list ):